#include "CalendarView.h"
#include "CalendarPanel.h"
#include "GroceryListPanel/GroceryListView.h"

CalendarView::CalendarView(CalendarPanel* panel) :
	panel(panel)
{
	for(int i = 0; i < 7; i++) {
		addAndMakeVisible(addButton[i] = new CalendarButton(T("+"), panel, i));
		addAndMakeVisible(minusButton[i] = new CalendarButton(T("-"), panel, i));
	}

	glow.setGlowProperties(3, Colours::black);
	addAndMakeVisible(createButton = new CalendarCreateListButton(String(T("Create Grocery List")), this));
}

CalendarView::~CalendarView()
{
	for(int i = 0; i < 7; i++) {
		delete addButton[i];
		delete minusButton[i];
	}
	delete createButton;
}

void CalendarView::paint(Graphics& g)
{
	Rectangle r(0, 0, getWidth(), getHeight() - 96);
	Image textImage(Image::ARGB, size, size, true);
	Graphics gfx(textImage);
	gfx.setFont(Font(Font::getDefaultSansSerifFontName(), 18, Font::plain));
	g.setFont(Font(Font::getDefaultSansSerifFontName(), 18, Font::plain));
	g.setColour(Colour(245, 250, 250));
	g.fillRect(r);
	g.setColour(Colours::grey);
	g.drawRect(r);

	//	Graphics textGFX(textImage);

	for(int i = 0; i < 7; i++) {
		Rectangle rect(getWidth() - 4 * size - 103, size * i + 6, size * 4 + 2, size - 7);

		g.setColour(Colour(253, 252, 250));
		g.fillRect(rect);
		g.setColour(Colours::grey);
		g.drawRect(rect);

		g.setColour(Colours::white);
		vector<Eatable>& meals = mealDays[i].getMeals();
		int count = 0;
		for(vector<Eatable>::const_iterator eit = meals.begin(); eit != meals.end(); ++eit, count++) {
			textImage.clear(0, 0, size, size);
			Image* image = eit->getImage();
			g.drawImage(image, count * size + rect.getX() + 2, rect.getY() + 2, size - 2, size - 11, 0, 0,
					image->getWidth(), image->getHeight(), false);

			gfx.drawMultiLineText(eit->getName(), 2, 15, size - 3);
			glow.applyEffect(textImage, gfx);
			g.drawImageAt(&textImage, count * size + rect.getX() + 2, size * i + 7);
			g.drawMultiLineText(eit->getName(), count * size + rect.getX() + 5, rect.getY() + 17, size - 3);
		}
		Rectangle rect2(7, rect.getY(), rect.getX() - 14, size - 7);

		g.setColour(Colour(245, 250, 250));
		g.fillRect(rect2);
		g.setColour(Colours::grey);
		g.drawRect(rect2);

		g.setColour(Colours::black);
		g.drawSingleLineText(String::formatted(T("Day %d"), i + 1), rect2.getX() + 5, rect2.getY() + 20);
		g.drawSingleLineText(String::formatted(T("Calories: %d"), getCalories(i)), rect2.getX() + 5,
				rect2.getY() + 40);

		//		g.drawSingleLineText(String(T("Recommendations:")), rect2.getX() + 5, rect2.getY() + 60);
		//		g.drawSingleLineText(String(T("Calorie:")), rect2.getX() + 5, rect2.getY() + 80);
		//		g.drawSingleLineText(String(T("Nutritional:")), rect2.getX() + 5, rect2.getY() + 100);
	}
	//	Rectangle r2(0, getHeight() - 92, getWidth(), 92);
	//
	//	g.setColour(Colour(245, 250, 250));
	//	g.fillRect(r2);
	//	g.setColour(Colours::grey);
	//	g.drawRect(r2);
}

int CalendarView::getCalories(int i)
{
	int calories = 0;
	for(vector<Eatable>::iterator eit = mealDays[i].getMeals().begin(); eit != mealDays[i].getMeals().end(); ++eit) {
		calories += (int)eit->getNutritionalInfo().calories;
	}
	return calories;
}

void CalendarView::resized()
{
	size = (int)((getHeight() - 100) * 0.142857f);
	for(int i = 0; i < 7; i++)
		addButton[i]->setBounds(getWidth() - 46, i * size + 6, 40, size - 10);
	for(int i = 0; i < 7; i++)
		minusButton[i]->setBounds(getWidth() - 92, i * size + 6, 40, size - 10);

	createButton->setBounds(7, getHeight() - 89, getWidth() - 14, 85);
}

void CalendarView::mouseMove(const MouseEvent& e)
{
	mouseX = e.x;
	mouseY = e.y;
}

void CalendarView::addToDay(int id, Eatable& meal)
{
	if(mealDays[id].getMeals().size() < 4)
		mealDays[id].getMeals().push_back(meal);
	repaint(0, size * id + 7, getWidth(), size * (id + 1) + 7);
}

void CalendarView::subtractFromDay(int id)
{
	if(!mealDays[id].getMeals().empty())
		mealDays[id].getMeals().pop_back();
	repaint(0, size * id + 7, getWidth(), size * (id + 1) + 7);
}

void CalendarView::createGroceryList()
{
	map<int, Recipe>& recipes = PersistentFoodList::instance().getRecipes();
	map<int, Eatable*>& foodMap = PersistentFoodList::instance().getFoodMap();

	glView->clearList();
	for(int i = 0; i < 7; i++) {
		cout << i << " " << mealDays[i].getMeals().size() << endl;
		for(vector<Eatable>::const_iterator eit = mealDays[i].getMeals().begin(); eit
				!= mealDays[i].getMeals().end(); ++eit) {
			cout << "id: " << eit->id << endl;

			vector<int> ids = recipes[eit->id].getFoodID();
			for(vector<int>::iterator iit = ids.begin(); iit != ids.end(); ++iit) {
				cout << *iit << endl;
				Eatable* eatable = foodMap[*iit];
				if(eatable) {
					glView->addItem(*eatable);
				}
			}
		}
	}
	panel->switchToGroceryList();
}

void CalendarView::setGroceryListView(GroceryListView* view)
{
	glView = view;
}
